package com.gxa.service.impl;

import com.gxa.common.JsonResult;
import com.gxa.common.R;
import com.gxa.common.exception.PSSException;
import com.gxa.mapper.GoodsMapper;
import com.gxa.pojo.*;
import com.gxa.service.PurchaseSellStockService;
import com.gxa.utils.DateUtils;
import io.swagger.models.auth.In;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: wangyifan
 * @Date: 2021/12/02 11:34
 * @Description: $end$
 * @Version: V1.0
 */
@Service
public class PurchaseSellStockServiceImpl implements PurchaseSellStockService {


    @Autowired(required = false)
    private GoodsMapper goodsMapper;

    @Override
    public R queryGoods(Integer currentPage, Integer pageSize) {
        R r = new R();
        Integer params1 =(currentPage-1)*pageSize;
        Integer params2 = pageSize;
        List<Goods> goodsList = goodsMapper.queryGoodsList(params1,params2);

        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(goodsList);
        return r;
    }

    @Override
    public R addGoods(Goods goods) {
        R r = new R();
        int result = goodsMapper.addGoods(goods);
        if(result<=0){ throw new PSSException("增加商品失败");}
        r.setCode(200);
        r.setMsg("增加成功");
        return r;

    }

    @Override
    public R updateGoods(Goods goods) {
        R r = new R();
        int result = goodsMapper.updateGoods(goods);
        if(result<=0){ throw new PSSException("修改商品失败");}

            r.setCode(200);
            r.setMsg("修改成功");
            return r;

    }

    @Override
    public R queryGoodsType(Integer gt_id) {
        R r = new R();

        List<Goodstype> goodsTypeList=goodsMapper.queryGoodsType(gt_id);

        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(goodsTypeList);
        return r;
    }

    @Override
    public R addGoodsTypeGt_pno(String gt_name) {
        R r = new R();
        int result = goodsMapper.addGoodsTypeGt_pno(gt_name);
        if(result<=0){ throw new PSSException("增加商品大类失败");}
            r.setCode(200);
            r.setMsg("增加成功");
            return r;


    }

    @Override
    public R addGoodsTypeGt_name(String gt_pno, String gt_name) {
        R r = new R();
        int result = goodsMapper.addGoodsTypeGt_name(gt_pno,gt_name);
        if(result<=0){ throw new PSSException("增加商品小类失败");}
            r.setCode(200);
            r.setMsg("增加成功");
            return r;

    }

    @Override
    public R updateGoodsTypeGt_pno(Integer gt_id, String gt_name) {
        R r = new R();
        int result = goodsMapper.updateGoodsTypeGt_pno(gt_id,gt_name);
        if(result<=0){ throw new PSSException("修改商品种类失败");}
            r.setCode(200);
            r.setMsg("修改成功");
            return r;

    }

    @Override
    public R updateGoodsTypeGt_name(Integer gt_id, String gt_pno, String gt_name) {
        R r = new R();
        int result = goodsMapper.updateGoodsTypeGt_name(gt_id,gt_pno,gt_name);
        if(result<=0){ throw new PSSException("修改失败");}
            r.setCode(200);
            r.setMsg("修改成功");
            return r;

    }

    @Override
    public R deleteGoodsType(Integer gt_id) {
        R r = new R();
        int result = goodsMapper.deleteGoodsType(gt_id);
        if(result<=0){ throw new PSSException("删除商品种类失败");}
            r.setCode(200);
            r.setMsg("删除成功");
            return r;

    }

    @Override
    public R queryPurchase(Integer currentPage, Integer pageSize) {
        R r = new R();
        Integer currentPage0 =(currentPage-1)*pageSize;
        List<Map> purchaseList = goodsMapper.queryPurchase(currentPage0,pageSize);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(purchaseList);
        return r;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R addPurchase(Purchase purchase) {
        R r = new R();
        Integer result1 =goodsMapper.addPurchase(purchase);
        if(result1<=0){throw new PSSException("添加采购记录失败");}//添加采购记录失败后回滚

        //添加采购记录成功后修改商品库存
        Integer goods_id = purchase.getGood_id();
        Integer pur_numb =purchase.getPur_numb();
        Integer result2 = goodsMapper.updateGoodsInventory(goods_id,pur_numb);
        r.setCode(200);
        r.setMsg("新增成功");
        return r;
    }

    @Override
    public R deletePurchase(Integer pur_id) {
        R r = new R();
        Integer result = goodsMapper.deletePurchase(pur_id);
        if(result<=0){ throw new PSSException("删除采购记录失败");}
            r.setCode(200);
            r.setMsg("删除成功");


        return r;
    }

    @Override
    public R querySupply(Integer currentPage, Integer pageSize) {

        R r = new R();
        Integer params1 =(currentPage-1)*pageSize;
        List<Supply> supplyList=goodsMapper.querySupply(params1,pageSize);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(supplyList);
        return r;
    }

    @Override
    public R addSupply(Supply supply) {
        R r = new R();
        Integer result = goodsMapper.addSupply(supply);
        if(result<=0){ throw new PSSException("增加供应商失败");}
        r.setCode(200);
        r.setMsg("增加成功");

        return r;
    }

    @Override
    public R searchSupply(String su_name, Integer su_cooperation, String  su_contact, String su_phone, String su_tel, String su_fax, String su_we, String su_qq, String su_email, String su_web, String su_pro, String su_city, Date add_date01, Date add_date02) {
        R r = new R();
        String addDate1 = DateUtils.date2String(add_date01);
        String addDate2 = DateUtils.date2String(add_date02);
        List<Supply> supplyList = goodsMapper.searchSupply(su_name,su_cooperation,su_contact,su_phone,su_tel,su_fax,su_we,su_qq,su_email,su_web,su_pro,su_city,addDate1,addDate2);
        r.setCode(200);
        r.setMsg("搜索成功");
        r.setData(supplyList);
        return r;
    }

    @Override
    public R deleteSupply(Integer su_id) {
        R r = new R();
        Integer result = goodsMapper.deleteSupply(su_id);
        if(result<=0){ throw new PSSException("删除供应商失败");}
        r.setCode(200);
        r.setMsg("删除成功");


        return r;
    }

    @Override
    public R updateSupply(Supply supply) {
        R r = new R();
        Integer result = goodsMapper.updateSupply(supply);
        if(result<=0){ throw new PSSException("修改供应商失败");}
            r.setCode(200);
            r.setMsg("修改成功");
        return r;
    }

    @Override
    public R queryStoreHouse() {
        R r = new R();
        List<Store_House> store_houseList = goodsMapper.queryStoreHouse();
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(store_houseList);
        return r;
    }

    @Override
    public R addStoreHouse(Store_House storeHouse) {
        R r = new R();
        Integer result = goodsMapper.addStoreHouse(storeHouse);
        if(result<=0){ throw new PSSException("增加仓库失败");}
            r.setCode(200);
            r.setMsg("增加成功");


        return r;
    }

    @Override
    public R updateStoreHouse(Store_House storeHouse) {
        R r = new R();
        if(storeHouse.getSh_id() == null || "".equals(storeHouse.getSh_id())){
            throw new PSSException("仓库id不存在");
        }
        Integer result = goodsMapper.updateStoreHouse(storeHouse);
        if(result<=0){ throw new PSSException("修改仓库信息失败");}
            r.setCode(200);
            r.setMsg("修改成功");

        return r;
    }

    @Override
    public R deleteStoreHouse(Integer sh_id) {
        R r = new R();
        Integer result = goodsMapper.deleteStoreHouse(sh_id);
        if(result<=0){ throw new PSSException("删除仓库失败");}
            r.setCode(200);
            r.setMsg("删除成功");

        return r;
    }

    @Override
    public  R  querySalesRecord(Integer currentPage, Integer pageSize) {
        R r = new R();
        Integer currentPage0 =(currentPage-1)*pageSize;
        List<Map> ordersGoodsList =goodsMapper.queryOrderGoods(currentPage0,pageSize);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(ordersGoodsList);
        return r;
    }

    @Override
    public R queryPayment() {
        R r = new R();
        List<Payment> paymentList = goodsMapper.queryPayment();
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(paymentList);
        return r;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult searchGoods(String gt_pno,String gt_name,String goods_name,String goods_descript,String goods_barcode,Integer currentPage,Integer pageSize) {
        JsonResult r = new JsonResult();
        Integer currentPage0 =(currentPage-1)*pageSize;
        List<Integer> gt_id = new ArrayList<>();

        if(gt_pno == null || "".equals(gt_pno)){
            gt_id = null;
        }else{
            gt_id = goodsMapper.queryGt_id(gt_pno,gt_name);
        }
        List<Goods> goodsList =goodsMapper.searchGoods(gt_id,goods_name,goods_descript,goods_barcode,currentPage0,pageSize);
        r.setData(goodsList);
        r.setCount(goodsMapper.countSearch(gt_id,goods_name,goods_descript,goods_barcode));
//        if(gt_pno == null || "".equals(gt_pno)){
//            List<Goods> goodsList =goodsMapper.searchGoods(gt_id,goods_name,goods_descript,goods_barcode,currentPage0,pageSize);
//            r.setData(goodsList);
//            r.setCount(goodsMapper.countSearch(gt_id,goods_name,goods_descript,goods_barcode));
//        }else if(gt_name == null || "".equals(gt_name)){
//            List<Goods> goodsList = new ArrayList<Goods>();
//            Integer count = 0;
//             gt_id = goodsMapper.queryGt_id(gt_pno,gt_name);
//                List<Goods> goodList =goodsMapper.searchGoods(gt_id,goods_name,goods_descript,goods_barcode,currentPage0,pageSize);
//                count += goodsMapper.countSearch(gt_id,goods_name,goods_descript,goods_barcode);
//
//
//            r.setData(goodsList);
//            r.setCount(count);
//        }else{
//            List<Integer> gtIdList = goodsMapper.queryGt_id(gt_pno,gt_name);
//            List<Goods> goodsList =goodsMapper.searchGoods(gtIdList,goods_name,goods_descript,goods_barcode,currentPage0,pageSize);
//            r.setData(goodsList);
//            r.setCount(goodsMapper.countSearch(gtIdList,goods_name,goods_descript,goods_barcode));
//            System.out.println(3);
//        }
//

        r.setCode(200);
        r.setMsg("搜索成功");
        return r;
    }

    @Override
    public R deleteGoods(Integer goods_id) {
        R r = new R();
        Integer result = goodsMapper.deleteGoods(goods_id);
        if(result<=0){ throw new PSSException("删除商品失败");}
            r.setCode(200);
            r.setMsg("删除成功");

        return r;
    }

    @Override
    public R batchDeleteGoods(List<Integer> goods_idList) {
        R r = new R();
        Integer result = goodsMapper.batchDeleteGoods(goods_idList);

        if(result<=0){ throw new PSSException("批量删除商品失败");}
            r.setCode(200);
            r.setMsg("批量删除成功");

        return r;
    }

    @Override
    public R searchSupply2(String string) {
        R r = new R();
        List<Supply> supplyList = goodsMapper.searchSupply2(string);
        r.setData(supplyList);
        r.setCode(200);
        r.setMsg("搜索成功");
        return r;
    }

    @Override
    public JsonResult countDataBase(String database) {
        JsonResult jsonResult =new JsonResult();
        if("goods".equals(database) || "supply".equals(database) || "store_house".equals(database) || "purchase".equals(database) || "order_goods".equals(database)){
            Integer count = goodsMapper.countDataBase(database);
            jsonResult.setCode(200);
            jsonResult.setCount(count);
            jsonResult.setMsg("查询成功");
        } else {
            jsonResult.setCode(400);
            jsonResult.setMsg("查询失败，表名错误");
        }
        return jsonResult;
    }
}
